<template>
  <div class="app-container">
    <el-table v-if="total>0" v-loading="listLoading" :data="list" border fit highlight-current-row style="width: 100%">

      <el-table-column align="center" label="序号" width="80">
        <template slot-scope="scope">
          <span>{{ scope.$index+(listQuery.page-1)*10+1 }}</span>
        </template>
      </el-table-column>

      <el-table-column width="180px" align="center" label="发布时间">
        <template slot-scope="scope">
          <span>{{ scope.row.blogTime }}</span>
        </template>
      </el-table-column>

      <el-table-column min-width="300px" label="标题">
        <template slot-scope="{row}">
          <router-link :to="'/operation/look/'+row.articleId" class="link-type">
            <span>{{ row.title }}</span>
          </router-link>
        </template>
      </el-table-column>

      <el-table-column width="180px" align="center" label="观看数">
        <template slot-scope="scope">
          <span>{{ scope.row.view }}</span>
        </template>
      </el-table-column>

      <el-table-column width="180px" align="center" label="评论数">
        <template slot-scope="scope">
          <span>{{ scope.row.commentCount }}</span>
        </template>
      </el-table-column>

      <el-table-column class-name="status-col" label="状态" width="110">
        <template slot-scope="{row}">
          <el-tag :type="row.blogType | statusFilter">
            {{ row.blogType ? '公开' : '私密' }}
          </el-tag>
        </template>
      </el-table-column>

      <el-table-column align="center" label="操作" width="270">
        <template slot-scope="scope">
          <router-link :to="'/operation/edit/'+scope.row.articleId">
            <el-button class="el-button" type="primary" size="small" icon="el-icon-edit">
              编辑
            </el-button>
          </router-link>
          <template>
            <el-popconfirm
              confirmButtonText="确定"
              cancelButtonText="取消"
              icon="el-icon-info"
              iconColor="red"
              title="确定删除这篇文章吗？"
              @onConfirm="deleteArticle(scope.row.articleId)"
              @onCancel="cancelEvent"
            >
              <template #reference>
                <el-button class="elBtn" type="primary" size="small" icon="el-icon-delete">
                  删除
                </el-button>
              </template>
            </el-popconfirm>
          </template>
        </template>
      </el-table-column>
    </el-table>
    <el-table v-else />
    <pagination v-show="total>10" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />

  </div>
</template>

<script>
import { deleteArt, fetchList } from '@/api/article'
import Pagination from '@/components/Pagination' // Secondary package based on el-pagination
export default {
  name: 'ArticleList',
  components: { Pagination },
  filters: {
    statusFilter(status) {
      const statusMap = {
        '1': 'success',
        '0': 'info'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      list: null,
      total: 0,
      articleId: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 10
      }
    }
  },
  computed: {
    id() {
      return this.$store.state.user.id
    }
  },
  created() {
    this.getList()
  },
  activated() {
    this.getList()
  },

  methods: {
    getList() {
      this.listLoading = true
      fetchList(this.id, this.listQuery.page, this.listQuery.limit).then(response => {
        const { data } = response
        this.total = data.blogCount
        if (this.total > 0) {
          this.list = data.userBlog
          this.articleId = data.userBlog.articleId
        }
        this.listLoading = false
      })
        .catch(error => {
          console.log(error)
        })
    },
    deleteArticle(id) {
      deleteArt(id).then(response => {
        this.$message({
          type: 'success',
          message: '文章删除成功',
          duration: 800
        })
        this.getList()
      })
        .catch(error => {
          this.$message({
            type: 'error',
            message: '文章删除失败'
          })
        })
    },
    cancelEvent(){
    }
  }
}
</script>

<style scoped>
.edit-input {
  padding-right: 100px;
}
.elBtn {
margin-left: 5px;
}
.cancel-btn {
  position: absolute;
  right: 15px;
  top: 10px;
}
</style>
